package com.bonorsoft.system.controller;

import com.bonorsoft.common.api.Result;
import com.bonorsoft.config.MyConfig;
import com.bonorsoft.system.entity.SysMoney;
import com.bonorsoft.system.service.SysMoneyService;
import com.bonorsoft.util.IPUtils;
import com.bonorsoft.system.sdk.WXPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class WXPayController {
    @Autowired
    private SysMoneyService moneyService;

    @PostMapping(value = "/wxpay")
    public Result<?> wxPay(@RequestBody SysMoney money, HttpServletRequest request) throws Exception {

        String ip = IPUtils.getIpAddr(request);

        long time = new Date().getTime();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        String fm = yyyyMMdd.format(new Date());
        String no = fm + time;
        System.out.println(no);
        money.setOutTradeNo(no);
        money.setDate(new Date());
        moneyService.save(money);


        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "腾讯充值中心-QQ会员充值");
        data.put("out_trade_no", no);
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", money.getMoney());
        data.put("spbill_create_ip", ip);
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
        data.put("openid", money.getOpenid());//of-Oq5bKPR6D7D_eMcdRvu1Ax-nc
        Map<String, String> resp = new HashMap<>();
        try {
            resp = wxpay.unifiedOrder(data);
            /*String returnCode = resp.get("return_code");
            if("SUCCESS".equals(returnCode)){
                System.out.println(resp);
                Integer times = (int) (new Date().getTime()/1000);
                Map<String, String> reqData = new HashMap<>();
                reqData.put("appId", config.getAppID());
                reqData.put("nonceStr", nonceStr);
                reqData.put("package", "prepay_id=" + resp.get("prepay_id"));
                reqData.put("signType", WXPayConstants.HMACSHA256);
                reqData.put("timeStamp", times + "");
                System.out.println(reqData);
                resp.put("sign",WXPayUtil.generateSignature(reqData, config.getKey(), WXPayConstants.SignType.HMACSHA256));
                resp.put("timeStamp",times + "");
                return Result.ok(resp);
            }else{
                return Result.error(resp.get("err_code_des"));
            }*/
        } catch (Exception e) {
            e.printStackTrace();
//            return Result.error(e.toString());
        }
        return Result.ok(resp);
    }

    /*public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", "201609091059590000002");

        try {
            Map<String, String> resp = wxpay.orderQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

/*    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", "4200000718202007256499142805");

        try {
            Map<String, String> resp = wxpay.refund(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

  /*  public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", "201609091059590000002");

        try {
            Map<String, String> resp = wxpay.closeOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/



}